package com.bigdata

import scala.io.Source

object Demo20WordCount {

  def main(args: Array[String]): Unit = {
    /**
     * 统计单词的数量
     */
    //1、读取文件
    val lines: List[String] = Source.fromFile("data/words.txt").getLines().toList

    // lines.foreach(println)

    //2、将一行中的多个单纯展开，变成一个单词一行
    val words: List[String] = lines.flatMap(line => {
      line.split(",")
    })

    // words.foreach(println)

    //3、按照单词分组
    val wordGroup: Map[String, List[String]] = words.groupBy(word => word)
    // wordGroup.foreach(println)

    /* val wordGroup: Map[String, List[String]] = words.groupBy(word => word.substring(0, 2))
     wordGroup.foreach(println)*/

    //4、统计单词的数量
    val result: Map[String, Int] = wordGroup.map((kv: (String, List[String])) => {
      val word: String = kv._1
      val count: Int = kv._2.size
      (word, count)
    })

    //打印结果
    result.foreach(println)

    println("=" * 100)


    // 简化上面的代码
    val result2: Map[String, Int] = lines
      .flatMap(_.split(","))
      .groupBy(word => word)
      .map(kv => {
        (kv._1, kv._2.length)
      })

    result2.foreach(println)


  }

}
